file(GLOB_RECURSE sources "*.cpp" "*.h")
set(CTEST_LIST)
set(TEST_ARGS "--testpath=${CMAKE_SOURCE_DIR}/test/data")
foreach(file ${sources})
    file(STRINGS ${file} test_list_raw REGEX "BOOST_.*TEST_(SUITE|CASE|SUITE_END)")
    set(TestSuite "DEFAULT")
    set(TestSuitePath "")
    foreach(test_raw ${test_list_raw})
        string(REGEX REPLACE ".*TEST_(SUITE|CASE)\\(([^ ,\\)]*).*" "\\1 \\2" test ${test_raw})
        if(test MATCHES "^SUITE .*")
            string(SUBSTRING ${test} 6 -1 TestSuite)
            set(TestSuitePath "${TestSuitePath}/${TestSuite}")
            if (NOT ";${allSuites};" MATCHES ";${TestSuite};")
                string(SUBSTRING ${TestSuitePath} 1 -1 TestSuitePathFixed)
                list(APPEND allSuites ${TestSuite})
                separate_arguments(TEST_ARGS)
                set(TestArgs -t ${TestSuitePathFixed} -- ${TEST_ARGS})
                list(APPEND CTEST_LIST ${TestSuitePathFixed})
                add_test(NAME ${TestSuitePathFixed} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                         COMMAND ./test/test_devkits ${TestArgs})
            endif()
        elseif(test MATCHES "^CASE .*")
            if(NOT test MATCHES "^CASE &createRandom.*")
                string(SUBSTRING ${test} 5 -1 TestCase)
                string(SUBSTRING ${TestSuitePath} 1 -1 TestSuitePathFixed)
                separate_arguments(TEST_ARGS)
                set(TestArgs -t ${TestSuitePathFixed}/${TestCase} -- ${TEST_ARGS})
                list(APPEND CTEST_LIST ${TestSuitePathFixed}/${TestCase})
                add_test(NAME ${TestSuitePathFixed}/${TestCase} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                         COMMAND ./test/test_devkits ${TestArgs})
            endif()
        elseif (";${test_raw};" MATCHES "BOOST_AUTO_TEST_SUITE_END()")
            string(FIND ${TestSuitePath} "/" Position REVERSE)
            string(SUBSTRING ${TestSuitePath} 0 ${Position} TestSuitePath)
        endif()
    endforeach()
endforeach()

message("-- CTest list")
foreach(test ${CTEST_LIST})
    message("--  -> ${test}")
endforeach()
message("------------------------------------------------------------------------")
message("")

add_executable(test_devkits ${sources})
target_include_directories(test_devkits PRIVATE ${PROJECT_SOURCE_DIR}/test)
target_link_libraries(test_devkits
        devcore initializer Boost::filesystem Boost::unit_test_framework Boost::program_options)